package com.ktjy;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
public class ActivityTester {
    //管理流程定义和部署
    @Autowired
    private RepositoryService repositoryService;
    //启动和管理流程实例
    @Autowired
    private RuntimeService runtimeService;
    // 查询和操作用户任务
    @Autowired
    private TaskService taskService;
    //    查询历史流程数据
    @Autowired
    private HistoryService historyService;

    //创建流程定义
    @Test
    public void test01() {
        Deployment deployment = repositoryService.createDeployment() //创建部署对象
                .addClasspathResource("bpmn/Activity2.0.bpmn20.xml") //添加资源文件
                .name("活动申请2..0")
                .deploy(); //部署
        System.out.println("流程部署成功，部署ID: " + deployment.getId());
    }

    //启动流程实例
    @Test
    public void test02() {
        Map<String, Object> variables = new HashMap<>();
//        variables.put("days", 5);
        variables.put("username", "张三");
//        String processDefinitionId = "Activity:1:555c0184-815d-11f0-858d-7215fba77862";
//        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinitionId, variables);
        String processDefinitionKey = "Activity2.0";
//        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinitionId);
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, variables);
        System.out.println("请假流程已启动，流程实例ID: " + processInstance.getId());
    }

    //    查询任务
    @Test
    public void test3() {
        System.out.println("=== 开始查询任务 ===");
        String assignee = "李大明";
        String processInstanceId = "d648e3f4-8224-11f0-a2d5-7215fba77862";  // 流程实例ID
        List<Task> tasks = taskService.createTaskQuery() //创建任务查询对象
                .taskAssignee(assignee) // 查询条件: 按照负责人查询
                .processInstanceId(processInstanceId) // 查询条件: 按照流程实例ID查询
                .active() //查询条件: 查询正在执行的任务
                .list();
        for (Task task : tasks) {
            System.out.println("=== 查询到的任务详情 ===");
            System.out.println("流程定义ID: " + task.getProcessDefinitionId());
            System.out.println("流程实例ID: " + task.getProcessInstanceId());
            System.out.println("任务ID: " + task.getId());
            System.out.println("任务名称: " + task.getName());
            System.out.println("办理人: " + task.getAssignee());
            System.out.println("创建时间: " + task.getCreateTime());
        }
    }

    //完成任务
    @Test
    public void test4() {
        System.out.println("=== 完成任务 ===");
        //添加批注
        // taskService.addComment("aa384e19-8162-11f0-93f4-7215fba77862","6ef3857a-815d-11f0-850a-7215fba77862","小伙子辛苦了");
        String taskId = "d648e3f4-8224-11f0-a2d5-7215fba77862";
        // 获取特定任务变量
//        Object days = taskService.getVariable(taskId, "days");
//        System.out.println("请假天数: " + days);
        // 设置审批结果变量
        Map<String, Object> approvalVariables = new HashMap<>();
        approvalVariables.put("outcomt", "同意");
        taskService.complete(taskId, approvalVariables);
    }

}
